﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class UserController : BaseAuthController
    {
         /// <summary>
        /// 返回Index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            string _departmentID = GetParam("j_depatmentID");
            string _userName = GetParam("j_username");
            List<object> result = new List<object>();
            UserBLL bll = new UserBLL();
            
            PagedList<EMOA_User> list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows),
                p => (string.IsNullOrEmpty(_departmentID) || p.DepartmentID == new Guid(_departmentID))&&
                (string.IsNullOrEmpty(_userName)||p.EmployeeName.Contains(_userName)));
            list.ForEach(p =>
            {
                string _departmentName = string.Empty;
                string _roleName = string.Empty;
                if (p.DepartmentID.HasValue)
                {
                    DepartmentBLL _departmentBLL = new DepartmentBLL();
                    _departmentName = _departmentBLL.GetEntity(p.DepartmentID.Value).DepartmentName;
                }
                result.Add(new { UserID = p.UserID, UserName = p.UserName, EmployeeName = p.EmployeeName, DepartmentID = _departmentName, RoleID = _roleName, Telephone = p.Telephone });
            });

            return ToJson(result, list.TotalItemCount);
        }

        /// <summary>
        /// 根据ID获取对象实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetEntity(Guid id)
        {
            UserBLL bll = new UserBLL();
            var data = new UserModels().ToModel<EMOA_User>(bll.GetEntity(id));

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(UserModels model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserBLL bll = new UserBLL();
                    EMOA_User entity = model.ToEntity<EMOA_User>();
                    entity.UserID = Guid.NewGuid();
                    entity.CreateBy = CurrentUser.UserID;
                    entity.CreateTime = DateTime.Now;
                    if (bll.Add(entity))
                    {
                        return Content("OK");
                    }
                    else
                    {
                        return Content("增加失败");
                    }
                }
                else
                    return Content("验证失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(UserModels model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserBLL bll = new UserBLL();
                    EMOA_User entity = bll.GetEntity(model.UserID);
                    entity = model.ToEntity<EMOA_User>(entity);
                    entity.UpdateBy = CurrentUser.UserID;
                    entity.UpdateTime = DateTime.Now;
                    if (bll.Update(entity))
                        return Content("OK");
                    else
                        return Content("修改失败");
                }
                else
                {
                    return Content("验证失败");
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                UserBLL bll = new UserBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("删除失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

    }
}